void *ReceiverForGame(void*)
{
	// Initialize Winsock.
	WSADATA wsaData;
	int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR)
		//printf("Server: Error at WSAStartup().\n");
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: Error at WSAStartup().");
	else
		Device->getGUIEnvironment()->addMessageBox(L"warning",L".GAME RECEIVER: WSAStartup() is OK.");

		//printf("Server: WSAStartup() is OK.\n");

	// Create a SOCKET for listening for
	// incoming connection requests.
	SOCKET ListenSocket;
	ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ListenSocket == INVALID_SOCKET)
	{
		//printf("Server: Error at socket(): %ld\n", WSAGetLastError());
		Device->getGUIEnvironment()->addMessageBox(L"warning",L".GAME RECEIVER: Error at socket():.");

		WSACleanup();
		system("pause");
		return 0;
	}
	else
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: socket() is OK");

		//printf("Server: socket() is OK.\n");

	// The sockaddr_in structure specifies the address family,
	// IP address, and port for the socket that is being bound.
	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr("127.0.0.1");
	service.sin_port = htons(123455);

	while(bind(ListenSocket, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR)
	{
		bind(ListenSocket, (SOCKADDR*) &service, sizeof(service));
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: bind() failed.");

		//printf("Server: bind() failed.\n");
		//closesocket(ListenSocket);
		//system("pause");
		//return 0;
	}
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: bind() is OK.");
		//printf("Server: bind() is OK.\n");

	// Listen for incoming connection requests on the created socket
	if (listen(ListenSocket, 10) == SOCKET_ERROR)
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: Error listening on socket.");
		//printf("Server: Error listening on socket.\n");
	else
		Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: listen() is OK.");
		//printf("Server: listen() is OK.\n");

	// Create a SOCKET for accepting incoming requests.
	SOCKET AcceptSocket;
	//printf("Server: Waiting for client to connect...\n");

	while(1)
	{
		// Accept the connection if any...
		while(1)
		{
			AcceptSocket = SOCKET_ERROR;
			while(AcceptSocket == SOCKET_ERROR)
			{
				AcceptSocket = accept(ListenSocket, NULL, NULL);
			}
			Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: accept() is OK.");
			//printf("Server: accept() is OK.\n");
			Device->getGUIEnvironment()->addMessageBox(L"warning",L"GAME RECEIVER: Client connected...ready for communication.");
			//printf("Server: Client connected...ready for communication.\n");
			break;
		}

		int bytesRecv = SOCKET_ERROR;
		
		int i = 0;

			// Receives some test string from client...and client
			// must send something lol...
			//Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"begin to receive",false);
			char recvbuf[200] = "";
		    std::string recvstr;
			//Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"begin to receive",false);
			bytesRecv = recv(AcceptSocket, recvbuf, 200, 0);
           // Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"ready to receive",false);

			if (bytesRecv == SOCKET_ERROR)
			{
				Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"GAME RECEIVER: recv() error",false);
				//printf("Server: recv() error %ld.\n", WSAGetLastError());
				continue;
			}
			else
			{
				recvstr = recvbuf;
				size_t len = strlen(recvbuf) + 1;
				size_t converted = 0;
				wchar_t *WStr;
				WStr=(wchar_t*)malloc(len*sizeof(wchar_t));
				mbstowcs_s(&converted, WStr, len, recvbuf, _TRUNCATE);
				//Device->getGUIEnvironment()->addMessageBox(L"Receive Message",WStr,false);
				RecvPosition = recvstr;
			}
	       // Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"Receive round ends",false);
			
		
		//WSACleanup();
	}
	Device->getGUIEnvironment()->addMessageBox(L"Receive Message",L"Receive process ends",false);
	pthread_exit(NULL);
	return NULL;
}
void *SenderForGame(void*)
{
	while(1)
	{
	if(ipstr.size() == 0)
	{
		//env->addMessageBox(L"Warning!",L"Empty IP address!",true);
		continue;
	}
	IGUIEnvironment* env = Device->getGUIEnvironment();

	env->addMessageBox(L"Warning!",L"Send game position!",false);
	int size = 0;
	char* ipadd;
	size = WideCharToMultiByte(CP_ACP,0,ipstr.c_str(),-1,NULL
		,0,NULL,NULL);
	ipadd = (char*)malloc(size + 1);
	WideCharToMultiByte(CP_ACP,0,ipstr.c_str(),-1,ipadd,
		size,NULL,NULL);
	WSADATA wsaData;

	int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

	if (iResult != NO_ERROR)
		env->addMessageBox(L"Warning!",L"Client: Error at WSAStartup().\n",false);
	   //printf("Client: Error at WSAStartup().\n");

	else
		env->addMessageBox(L"Warning!",L"Client: WSAStartup() is OK.\n",false);
	//printf("Client: WSAStartup() is OK.\n");
	// Create a SOCKET for connecting to server

	SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (ConnectSocket == INVALID_SOCKET)
	{
		//printf("Client: Error at socket(): %ld.\n", WSAGetLastError());
		WSACleanup();
	//	return 0;
	}
	//else
	//printf("Client: socket() is OK.\n");
	// The sockaddr_in structure specifies the address family,
	// IP address, and port of the server to be connected to.

	sockaddr_in clientService;
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");//a fixed server ip add
	clientService.sin_port = htons(543211);// a fixed port number used to communicate with the server
	// Connect to server.

	while(connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
	{
		connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
		env->addMessageBox(L"Warning!",L"Client: Failed to connect.\n",false);
		//printf("Client: Failed to connect.\n");
		//WSACleanup();
	//	return 0;
	}
	else
		env->addMessageBox(L"Warning!",L"Client:connect() is OK \n",false);
	//printf("Client: connect() is OK.\n");
	while(1)
	{
		int bytesSent;
		int bytesRecv = SOCKET_ERROR;
		char* sendbuf = "";
		std::string sendstr("");
		sendstr = SendPosition;
		if(SendPosition.empty())
		{
			env->addMessageBox(L"Error",L"SendPosition is empty!",false);
			continue;
		}

		sendbuf = (char*)sendstr.c_str();
		std::string ipstdstr("");
		ipstdstr = ipadd;
		//env->addMessageBox(L"Message sent",(wchar_t*)ipstdstr.c_str(),false);
		ipstdstr.append(" ");
		sendstr.insert(0,ipstdstr);
		// Send and receive data.

		bytesSent = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
		if(bytesSent == 0)
			env->addMessageBox(L"Error",L"Sent Error!",false);
		size_t len = strlen(sendbuf) + 1;
		size_t converted = 0;
		wchar_t *WStr;
		WStr=(wchar_t*)malloc(len*sizeof(wchar_t));
		mbstowcs_s(&converted, WStr, len, sendbuf, _TRUNCATE);
		env->addMessageBox(L"Message sent",WStr,false);
		//env->addMessageBox(L"Message recv",(wchar_t*)recvstr.c_str(),false);
		//printf("Client: Bytes sent: %ld\n", bytesSent);
	}
	WSACleanup();
	}
}